package action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import actionform.ReplyForm;
import dao.ReplyMagDao;
import dao.impl.ReplyMagImpl;
import domain.Reply;
import domain.User;

/**
 * 提问和讨论回复的action
 * 
 * May 19, 2009
 *
 * @author Fabian
 *
 */
public class ReplyManagerAction extends DispatchAction {
	/**
	 * 浏览某个主题的所有回复，但是要传递分页参数（start--开始，limit--间隔）和主题的id
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		ReplyForm rf = (ReplyForm)form;
		int topic_id = rf.getTopic_id();
		int start = rf.getStart();
		int limit = rf.getLimit();
		
		ReplyMagDao rmd = new ReplyMagImpl();
		//获取此页面的回复
		List<Reply> list = rmd.getPageReply(start, limit, topic_id);
		//获取总记录数
		int totalCount = rmd.getReplyTotalCount(topic_id);
		
		//组装JSON字符串，返回
		String resultArray = "";
		for (Iterator<Reply> it = list.iterator(); it.hasNext(); ) {
			Reply r = it.next();
			Date d = r.getPubDate();
			long tt = d.getTime();
			resultArray = resultArray + "{id: " + r.getId() + ", author: '" + r.getAuthor() +
							"', role: '" + r.getRole() + "', pubdate: new Date(" + tt +
							"), content: '" + r.getContent() + "'}";
			if (it.hasNext()) {
				resultArray = resultArray + ",";
			}			
		}
		
		String jsonStr = "";
		jsonStr = "{totalProperty: " + totalCount + ", root: [ " + resultArray + " ]}";
		
		request.setAttribute("result", jsonStr);
		
		return mapping.findForward("show_result");
	}
	
	/**
	 * 发表某个主题的回复，传入主题的id
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ReplyForm rf = (ReplyForm)form;
		Reply r = new Reply();
		r.setTopic_id(rf.getTopic_id());
		r.setContent(rf.getContent());
		
		String flag = request.getParameter("flag");
		
		//获取发布人的信息
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		if (user == null) {
			return null;
		}
		
		r.setUser_id(user.getId());
		r.setRole_id(user.getRole_id());
		
		ReplyMagDao rmd = new ReplyMagImpl();
		boolean result = rmd.addReply(r);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			if ("question".equals(flag)) {
				returnStr = "{success: true, msg: '恭喜，回答问题成功！'}";
			} else if ("discuss".equals(flag)) {
				returnStr = "{success: true, msg: '恭喜，参与讨论成功！'}";
			}			
			request.setAttribute("result", returnStr);
			
		} else {
			if ("question".equals(flag)) {
				returnStr = "{failure: true, msg: '抱歉，回答问题失败！请重新尝试！'}";
			} else if ("discuss".equals(flag)) {
				returnStr = "{failure: true, msg: '抱歉，参与讨论失败！请重新尝试！'}";
			}			
			request.setAttribute("result", returnStr);
			
		}		
		return mapping.findForward("add_result");
	}
	
	/**
	 * 删除某个回复，传入回复id
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ReplyForm rf = (ReplyForm)form;
		
		int del_id = rf.getId();
		
		ReplyMagDao rmd = new ReplyMagImpl();
		boolean result = rmd.delReply(del_id);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{result: 'success', msg: '恭喜，删除回复成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{result: 'failure', msg: '抱歉，删除回复失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}	
		
		return mapping.findForward("del_result");
	}
}
